<template>
    <el-dialog v-dialog title="审核" :close-on-click-modal="false" :visible.sync="visible" @close="handleClose" :center="true">
        <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
            <el-form-item label="手机号">
                <span>
                    {{ dataForm.phone }}
                </span>
            </el-form-item>

            <el-form-item label="帐号">
                <span>{{ dataForm.userId }}</span>
            </el-form-item>
            <el-form-item label="角色" prop="roleType">
                <el-select v-model="dataForm.merchantType" placeholder="请选择">
                    <el-option v-for="(item, index) in roleOptions" :key="index" :label="item.rolename" :value="item.roleId"> </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="类型">
                <el-select v-model="dataForm.userType" placeholder="请选择">
                    <el-option v-for="(item, index) in approves" :key="index" :label="item.lable" :value="item.userType"> </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="审核意见">
                <el-input v-model="dataForm.approveAdvise" placeholder="输入审核意见" type="textarea"  clearable></el-input>
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dataFormSubmit(1)">通过</el-button>
            <el-button type="primary" @click="dataFormSubmit(2)">拒绝</el-button>
            <el-button @click="visible = false">取消</el-button>
        </span>
    </el-dialog>
</template>
<script>
import API from '@/api';
import cloneDeep from 'lodash/cloneDeep';
import { commercialUserType, userType } from '../../utils/util';

export default {
    data() {
        return {
            visible: false,
            dataForm: {
                roleType: ''
            },
            roleOptions: [],
            approves: [{userType: 'PERSON', lable: '个人'},{userType: 'CORP',lable: '企业'}],
            roleTypeName: '',
        };
    },

      filters: {
        // 商户类型
        commercialype(val) {
            // console.log(val);
            return commercialUserType(val);
        },
        // 用户类型
        userTypeTranslate(val) {
            return userType(val);
        }
    },
    
    methods: {
        handleClose() {
            this.dataForm = {};
            this.$refs['dataForm'].resetFields();
        },
        getDetail(id) {
            API.nzshManger
                .getAuditDetail({
                    userId: id
                })
                .then((res) => {
                    // console.log(res);
                    this.dataForm = res.result;
                    this.getroleList();
                });
        },
        init(id) {
            this.visible = true;
            this.getDetail(id);
        },

        // 通过或拒绝
        dataFormSubmit(type) {
            if (type == 2 && this.dataForm.approveAdvise == '') {
                // 拒绝
                this.$message.error('请填写审核意见，并说明拒绝原因');
                return;
            }

            var params = {
                approveAdvise: this.dataForm.approveAdvise,
                userId: this.dataForm.userId
            };
            var userParams = {
                userType: this.dataForm.userType,
                merchantType: this.dataForm.merchantType,
                userId: this.dataForm.userId
            };
            
            if (type == 1) {
                API.nzshManger.approveAudit(params).then((data) => {
                    if (data.code == '0000') {
                        //修改商户信息
                        API.nzshManger.updateUserInfo(userParams).then((data) => {
                            if (data.code == '0000') {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false;
                                        this.$emit('refreshDataList');
                                    }
                                });
                            }
                        });
                    }
                });
            } else if (type == 2) {
                API.nzshManger.rejectAudit(params).then((data) => {
                    if (data.code == '0000') {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false;
                                this.$emit('refreshDataList');
                            }
                        });
                    }
                });
            }
        },
        getroleList(item) {
            //选择角色
            var data = {};
            API.role.getWebRoleList(data).then((res) => {
                this.roleOptions = res.result;
            });
        },
    }
};
</script>